<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests\Admin\AppRequest;
use App\Http\Services\Admin\AppService;

use App\App as AppModel;

class AppController extends BaseController
{
    protected $indexUrl = 'admin/app';
    protected $indexItemList = [
        ["title"=>"应用名称", "field"=>"app_name"],
        ["title"=>"app_id", "field"=>"app_id"],
        ["title"=>"app_secret", "field"=>"app_secret"],
        ["title"=>"状态", "field"=>"status_text"],
    ];
    protected $indexKeyword = "应用名称";

	public function __construct(AppService $service, AppModel $model)
    {
    	parent::__construct($service);
        $this->model = $model;
        $this->formItemList = [
            ["control"=>"input","name"=>"应用名称", "field"=>"app_name"],
            ["control"=>"input","name"=>"应用描述", "field"=>"app_desc"],
            ["control"=>"select","name"=>"状态", "field"=>"status", "options"=>$this->getStatusList()],
        ];
    }

    public function create(){
        return $this->_create(['statusList'=>$this->model->statusList]);
    }

    public function edit($id){
        return $this->_edit($id, ['statusList'=>$this->model->statusList]);
    }

    // 存储
    public function store(AppRequest $request){
        $request['app_id'] = $this->service->createAppId();
        $request['app_secret'] = $this->service->createAppSecret();
        return $this->_store($request);
    }

    // 更新
    public function update(AppRequest $request, $id){
        return $this->_update($request, $id);
    }

    private function getStatusList(){
        $list = [];
        foreach($this->model->statusList as $key=>$value){
            array_push($list, ["value"=>$key, "text"=>$value]);
        }
        return $list;
    }
}
